<template>
  <el-dialog
    width="900px"
    :title="$t('店铺库存记录')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel"
  >
    <el-row style="margin-bottom: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
        <el-form-item prop="type">
          <el-select @change="getList" clearable :placeholder="$t('类型')" v-model="queryParams.type">
            <el-option
              v-for="item in sku_shop_record_type_list"
              :key="item.dictValue"
              :value="item.dictValue"
              :label="item.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="shopSku">
          <el-input v-model="queryParams.shopSku" :placeholder="$t('店铺sku')" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-table
      :height="calTableHeight() - 150"
      ref="TableRef"
      :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)"
    >
      <el-table-column show-overflow-tooltip align="center" :label="$t('店铺sku')" prop="shopSku" />
      <el-table-column align="center" show-overflow-tooltip prop="imgUrl" :label="$t('图片')">
        <template slot-scope="{ row }">
          <el-image
            style="width: 40px; height: 40px; object-fit: cover"
            :preview-src-list="[row.imgUrl]"
            :src="row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('类型')" prop="type">
        <template #default="{ row }">
          <div :style="{ color: $randomColor(row.type) }">{{ handleType(row.type) }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('变化数量')" prop="num" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('变化后数量')" prop="afterNum" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('出/入')" prop="inOut">
        <template #default="{ row }">
          <!-- 出/入 1-增加 2:减少 -->
          <div v-if="row.inOut == 1" style="color: green">{{ $t('增加') }}</div>
          <div v-if="row.inOut == 2" style="color: red">{{ $t('减少') }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('创建时间')" prop="createTime" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer">
      <el-button @click="cancel">
        {{ $t('关闭') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { skuShopRecordsList } from '@/api/distribution/sku/storeSkuList'
import { useDict } from '@/utils/useDict'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      sku_shop_record_type_list: [],
      /**表格数据 */
      tableData: [],
      loading: false,
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        shopSku: '',
        pageNum: 1,
        pageSize: 20,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
        useDict('sku_shop_record_type').then(({ sku_shop_record_type }) => {
          this.sku_shop_record_type_list = sku_shop_record_type
        })
      }
    },
  },
  methods: {
    handleType(type) {
      const index = this.sku_shop_record_type_list.findIndex((i) => i.dictValue == type)
      return this.sku_shop_record_type_list[index]?.dictLabel
    },
    handleSubmit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
        }
      })
      this.cancel()
    },
    getList() {
      this.loading = true
      const data = {
        ...this.queryParams,
        skuShopId: this.formData.id,
      }
      skuShopRecordsList(data)
        .then((res) => {
          this.tableData = res.rows
          this.total = res.total
        })
        .finally(() => (this.loading = false))
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 20
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
